# -*- coding: utf-8 -*-
"""
待完善
该代码应该在运行完naiveHotFactor, naiveHotFactor2, weightedHotFactor, weightedHotFactor2之后执行

因为naiveHotFactor与weightedHotFactor中的股票列表是历史上所有出现过的股票，但是在实际检验中，我们要按照
真是情况，只计算每天出现过的股票，所以将当天没有出现过的股票应该要过滤掉。

每天的股票信息在Daily_A_Stocks_List中
"""
import os

# 所有的统计数据都在该路径下
dataPath = r'E:\yinzm\nlp\data'
# filter文件路径
filterPath = '../Daily_A_Stocks_List/Daily_A_Stocks_List'
dir = input("因子文件名：")

factorPath = os.path.join(dataPath, dir)
# 准备好输出的路径
output_path = os.path.join(r'E:\yinzm\nlp\data2', dir)
os.makedirs(output_path)

for dateFile in os.listdir(factorPath):
    date = dateFile[:-4]
    if date < "20130101" or date > "20171231":
        continue
    print(date)
    factorFile = os.path.join(factorPath, dateFile)
    stock_factor = {} #将因子值读入
    with open(factorFile, 'r', encoding='utf-8') as f:
        for eachLine in f.readlines():
            stockCode, factorVal = eachLine.strip().split(',')
            stock_factor[stockCode] = factorVal

    filterFile = os.path.join(filterPath, date+'.txt')
    stock_list = [] # 今天出现的所有的股票代码
    with open(filterFile, 'r', encoding='utf-8') as f:
        for eachLine in f.readlines():
            stock_list.append(eachLine.strip())
    # 按照filter对原有的因子值进行过滤，写回
    with open(os.path.join(output_path, dateFile), 'w', encoding='utf-8') as f:
        for stockCode in stock_list:
            f.write("%s,%s\n" % (stockCode, stock_factor[stockCode]))
